package org.mao.forumtest;

import java.io.File;
import java.io.FileWriter;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ReadLwgodSection {

	private final static Logger logger;

	static {
		PropertyConfigurator.configure("D:/workspace/spiderman/src/main/resources/conf/log4j.properties");// 加载.xml文件
		logger = Logger.getLogger(ReadLwgodSection.class);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		logger.info("go");
		ReadLwgodSection read = new ReadLwgodSection();
		try {
			//read.readHomePage("http://www.lwgod.com/forum-292-1.html");
			read.readHomePageTxt("G:/download/lwgod/section.txt");
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	public void readHomePage(String url) throws Exception {
		File file = new File("G:/download/lwgod/section.txt");
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		FileWriter fw = new FileWriter(file);

		Document doc = Jsoup.connect(url).get();
		// logger.info(doc.html());
		fw.write(doc.html());
		fw.close();
	}

	public void readHomePageTxt(String path) throws Exception {
		int count = 0;
		Document doc = Jsoup.parse(new File(path), "UTF-8");
		Elements tables = doc.select("#threadlisttableid");
		if (CollectionUtils.isNotEmpty(tables)) {
			Element table = tables.get(0);
			Elements tbodys = table.select("tbody");
			if (CollectionUtils.isNotEmpty(tbodys)) {
				for (Element tbody : tbodys) {
					Elements ths = tbody.select("th");
					if (CollectionUtils.isNotEmpty(ths)) {
						Element th = ths.get(0);
						Elements links = th.select(">a");
						if (CollectionUtils.isNotEmpty(links)) {
							for (Element link : links) {
								String href = link.attr("abs:href");
								if(link.hasAttr("id")){
									System.out.println(link.attr("id"));
								}
								if(href!=null && href.endsWith("html")){
									System.out.println(link.text());
									System.out.println(href);
									count++;
								}
							}
						}
					}
				}
			}
		}
		System.out.println(count);
	}

}
